package com.ykdl.member.kid.marketcard;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import oauth.signpost.http.HttpParameters;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.BitmapUtils;
import com.sitech.oncon.barcode.core.CaptureActivity;
import com.ykdl.member.constant.KidAction;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.beans.ResultBean;
import com.ykdl.member.kid.beans.StoreInfoJsonBean;
import com.ykdl.member.kid.beans.Store_ListBean;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.gears.MyGoodLifeActivity;
import com.ykdl.member.kid.marketcard.NearbyCardActivity.getDataTag;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

public class CardInfoActivity  extends BaseScreen{
	private String store_id ,storename;
	private TextView card_name,store_name,member_right,address,business_scope,phone;
	private Button go_ad;
	private boolean gowhere=false;
	private RelativeLayout card_bg  ;
	private BitmapUtils bitmapUtil ;
	private String isGoneButton;
	private RelativeLayout store_goods_bg,store_coupon_bg;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTopTitle("", 0, R.drawable.back_bg, new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				finish();
			}
		}, 0, 0, null, R.drawable.title_bg, false);
		store_id = this.getIntent().getStringExtra(KidAction.STORE_ID);
		isGoneButton =this.getIntent().getStringExtra("from");
		setContent(R.layout.cardinfoactivity_xml);
		inint();
		getData();
	}
	public void inint() {
		go_ad = (Button)findViewById(R.id.go_ad);
		if(isGoneButton.equals("true")){
			go_ad.setVisibility(View.GONE);
		}
		go_ad.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (gowhere) {
					addCard();
				}else {
					go(CaptureActivity.class,"isShow",false);
				}
			}
		});
		card_name = (TextView)findViewById(R.id.card_name);
		store_name = (TextView)findViewById(R.id.store_name);
		member_right = (TextView)findViewById(R.id.member_right);
		address = (TextView)findViewById(R.id.address);
		business_scope = (TextView)findViewById(R.id.business_scope);
		phone = (TextView)findViewById(R.id.phone);
		bitmapUtil = new BitmapUtils(this);
		bitmapUtil.configDefaultLoadingImage(R.drawable.test_bg);
		bitmapUtil.configDefaultLoadFailedImage(R.drawable.test_bg);
		card_bg = (RelativeLayout)findViewById(R.id.card_bg);
		store_goods_bg=(RelativeLayout) findViewById(R.id.store_goods_bg);
		
		store_goods_bg.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(CardInfoActivity.this,NewStoreCoupon.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.putExtra(KidAction.STORE_ID, store_id+"");
				intent.putExtra(KidAction.STORE_NAME, storename+"");
				CardInfoActivity.this.startActivity(intent);
			}
		});
		store_coupon_bg=(RelativeLayout) findViewById(R.id.store_coupon_bg);
		store_coupon_bg.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(CardInfoActivity.this,NewStoreCoupon.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.putExtra(KidAction.STORE_ID, store_id+"");
				intent.putExtra(KidAction.STORE_NAME, storename+"");
				intent.putExtra(KidAction.IS_STORE, false);
				CardInfoActivity.this.startActivity(intent);
			}
		});
	}
	public void setData(final StoreInfoJsonBean bean) {
		storename=bean.getStore().getName();
		setTopNav(bean.getStore().getName());
		
		gowhere = bean.getNeed_receive();
		if (bean.getNeed_receive()) {
			go_ad.setText("立即添加门店");
		}else {
			go_ad.setText("扫描二维码添加门店");
		}
		if (null!=bean.getStore().getFile()) {
			bitmapUtil.display(card_bg, bean.getStore().getFile().getFile_url());	
		}else {
			card_bg.setBackgroundResource(R.drawable.test_bg);
		}
		card_name.setText(bean.getStore().getCard_name());
		store_name.setText(bean.getStore().getAddress());
		member_right.setText(bean.getStore().getMember_right());
		address.setText("");
		business_scope.setText("经营范围:"+bean.getStore().getBusiness_scope());
		phone.setText("");
		findViewById(R.id.call_phone).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (null!=bean.getStore().getPhone()) {
					Intent intent_phone = new Intent(Intent.ACTION_CALL,
							Uri.parse("tel:"+bean.getStore().getPhone()));
					CardInfoActivity.this.startActivity(intent_phone);
				}
				
			}
		});
	}
	public void addCard() {
		showProgress("正在加载中,请稍等...");
		String  pageurl =KidConfig.ADD_CARD;
		HttpParameters parameterss = new HttpParameters();
		parameterss.put("store_id", store_id);
		HttpBaseRequest request  =  Wxxr.getInstance().postBaseRequest(pageurl,parameterss,null);
		 TaskManager.startHttpRequest(request, new addCardTag(),  ResultBean.class);
	}
	
	class addCardTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			if (t instanceof ResultBean) {
				ResultBean bean = (ResultBean)t;
				if (null==bean.getError()) {
					Toast.makeText(mContext, "添加成功", 1).show();
				}else {
					Toast.makeText(mContext, bean.getDesc(), 1).show();
				}
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "失败", 1).show();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
	public void getData() {
		showProgress("正在加载中,请稍等...");
		String  pageurl =KidConfig.STORE_INFO;
		HttpParameters parameterss = new HttpParameters();
		parameterss.put("store_id", store_id);
		HttpBaseRequest request  =  Wxxr.getInstance().postBaseRequest(pageurl,parameterss,null);
		 TaskManager.startHttpRequest(request, new getDataTag(),  StoreInfoJsonBean.class);
	}
	
	class getDataTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			if (t instanceof StoreInfoJsonBean) {
				StoreInfoJsonBean bean = (StoreInfoJsonBean)t;
				if (null==bean.getError()) {
					setData(bean);
				}
				
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "加载失败", 1).show();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
}
